/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.main;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import org.jdesktop.layout.GroupLayout;
import org.tzconverter.about.AboutView;
import org.tzconverter.gui.GradientPanel;
import org.tzconverter.opacity.level.OpacityLevelView;
import org.tzconverter.patterneditor.PatternEditorPanel;
import org.tzconverter.tzgroup.TimeZoneGroupPanel;
import org.tzconverter.util.AWTUtilities;
import org.tzconverter.util.Icons;

/**
 * The main frame of the Time Zone Converter application.  This is the central tie-in for
 * all of the triads.  All of the triads are ultimately children of the MainFrame.
 * <p>
 * This class also contains the application's main method.  The main method 
 * loads the Spring context, gets the properly constructed MainFrame from the
 * context, and displays it.
 * </p>
 * <p>
 * TODO Provide an undo option for the TimeZoneDisplays.
 * TODO When the mouse is over a particular TimeZoneDisplay, highlight that TimeZoneDisplay somehow.
 * TODO Allow the user to make the application semi-translucent.
 * TODO Allow the user to save the current configuration as the default
 * TODO Improve visual appearance.
 * TODO Allow the user to display a dialog box with additional details about a time zone.
 * TODO Allow the user to change the order of the time zones
 * TODO Provide a drop-down or auto completion of valid date/time patterns
 * TODO Provide Eclipse-like auto completion for Time Zone Ids
 * </p>
 * @since Apr 19, 2009
 */

public class MainFrame extends JFrame implements MainView
{
  private static final long serialVersionUID = 3428825644451436966L;
    
  private final TimeZoneGroupPanel timeZoneGroupPanel;
  private final JPanel addTimeZonePanel;
  private final PatternEditorPanel patternEditorPanel;
  private final AWTUtilities awtUtils;
  
  private OpacityLevelView opacityLevelDialog;
  private AboutView aboutDialog;

  public MainFrame(TimeZoneGroupPanel timeZoneGroupPanel, 
      PatternEditorPanel patternEditorPanel, 
      JMenuBar mainMenu,
      AWTUtilities awtUtils,
      Icons icons) {
    super("TimeZoneConverter");

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    this.timeZoneGroupPanel = timeZoneGroupPanel;
    this.addTimeZonePanel = timeZoneGroupPanel.getAddPanel();
    this.patternEditorPanel = patternEditorPanel; 
    this.awtUtils = awtUtils;
  
    this.setIconImages(icons.getClocks());
    this.setJMenuBar(mainMenu);
    
    layoutComponents();
     
    this.pack();
    this.setMinimumSize(this.getPreferredSize()); 
    this.setLocationByPlatform(true);
  }
 
  private void layoutComponents()
  { 
    Container contentPane  = new GradientPanel(); 
    this.setContentPane(contentPane);
    
    GroupLayout layout = new GroupLayout(contentPane);
    contentPane.setLayout(layout);
    
    layout.setAutocreateContainerGaps(true);
    layout.setAutocreateGaps(true);
    
    layout.setHorizontalGroup(
        layout.createParallelGroup()
          .add(
              layout.createSequentialGroup()
                .add(patternEditorPanel)
                .add(addTimeZonePanel)
              )
          .add(timeZoneGroupPanel)
        );
    
    layout.setVerticalGroup(
        layout.createSequentialGroup()
          .add(
              layout.createParallelGroup(GroupLayout.BASELINE)
                .add(patternEditorPanel)
                .add(addTimeZonePanel)
              )
          .add(timeZoneGroupPanel)
        );
  }
  
  public void setOpacityLevelDialog(OpacityLevelView opacityLevelDialog)
  {
    this.opacityLevelDialog = opacityLevelDialog;
  }
  
  @Override
  public void showOpacityLevelDialog()
  {
    if(opacityLevelDialog != null)
    {
      opacityLevelDialog.setVisible(true);
    }
  }
  
  public void setAboutDialog(AboutView aboutView)
  {
    aboutDialog = aboutView; 
  }
  
  /* (non-Javadoc)
   * @see org.tzconverter.main.MainView#showAboutDialog()
   */
  @Override
  public void showAboutDialog()
  {
    if(aboutDialog != null)
    {
      aboutDialog.setVisible(true);
    }
  }

  @Override
  public void close()
  {
    System.exit(0);
  }
    
  @Override
  public void setOpacityLevel(float level)
  {
    awtUtils.setWindowOpacity(this, level);
  }
  
  @Override
  public boolean isTranslucencySupported()
  {
    return awtUtils.isTranslucencySupported();
  }
}
